package plugin;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import helloworld.Employee;
import helloworld.EmployeeMapper;

public class TestPageHelper {

    @Test
    public void testPageHelper1() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();

        try {
            EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
            
            // 设置查询页码与每页显示数量
            Page<Employee> page = PageHelper.startPage(1, 3);
            List<Employee> employees = employeeMapper.getEmployees();
            System.out.println(employees);
            System.out.println(page);
        } finally {
            session.close();
        }
    }
    
    @Test
    public void testPageInfo1() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();

        try {
            EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
            
            // 设置查询页码与每页显示数量
            PageHelper.startPage(4, 1);
            List<Employee> employees = employeeMapper.getEmployees();
            // 传入得到的集合与显示的页码
            PageInfo<Employee> pageInfo = new PageInfo<Employee>(employees, 3);
            // 得到显示的页码
            for(int num: pageInfo.getNavigatepageNums()){
                System.out.println(num);
            }
        } finally {
            session.close();
        }
    }
}
